package com.example.weather_plus.ui.weathergram.Items;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.weather_plus.R;
import com.example.weather_plus.bmob.Entries;
import com.example.weather_plus.ui.weathergram.view.DiaryActivity;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class EntriesCardFragment extends Fragment {

    static boolean flag = false;
    Card now_play;

    public EntriesCardFragment(Card now_play) {
        this.now_play = now_play;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_entries_card, container, false);

        TextView tv = view.findViewById(R.id.textView);                 // 呈现内容的textView布局
        TextView mo = view.findViewById(R.id.monthTextView);            // 月份
        TextView dd = view.findViewById(R.id.dateTextView);             // 日期
        TextView wt = view.findViewById(R.id.weekAndTimeTextView);      // 星期和时间

        // 底部功能按钮
        // TODO:实现底部按钮功能
        ImageButton button_1 = view.findViewById(R.id.button1);
        ImageButton button_2 = view.findViewById(R.id.button2);
        ImageButton button_3 = view.findViewById(R.id.button3);
        ImageButton button_4 = view.findViewById(R.id.button4);
        ImageButton button_5 = view.findViewById(R.id.button5);


        tv.setText(this.now_play.getContext());
        mo.setText(this.now_play.getMonth_long());
        dd.setText(this.now_play.getDate());
        wt.setText(this.now_play.getWeek_long() + " " + this.now_play.getTime());


        // 回退按钮
        ImageButton exit_button = view.findViewById(R.id.exit_button);

        // 设置点击事件为回退
        exit_button.setOnClickListener(v -> {
            // 处理点击事件，关闭当前Fragment
            if (getActivity() != null) {
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.remove(this); // 移除当前Fragment
                transaction.commit(); // 提交事务
            }
        });

        button_1.setOnClickListener(v -> {
            Toast.makeText(this.getActivity(), "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });
        button_2.setOnClickListener(v -> {
            Toast.makeText(this.getActivity(), "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });
        button_3.setOnClickListener(v -> {
            Toast.makeText(this.getActivity(), "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });
        button_4.setOnClickListener(v -> {
            Toast.makeText(this.getActivity(), "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });

        // 删除
        button_5.setOnClickListener(v -> {

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("删除确认");
            builder.setMessage("确定要删除吗?");
            builder.setPositiveButton("确定", (dialog, which) -> {
                // 执行删除操作
                Entries p2 = new Entries();
                p2.setObjectId(now_play.getEntries_id());
                p2.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(getActivity(), "删除成功，切换视图以刷新", Toast.LENGTH_SHORT).show();

                            // 更新UI
                            DiaryActivity entry = (DiaryActivity) getActivity();
                            entry.flash();
                        } else {
                            // 处理删除失败的情况
                        }
                    }
                });
            });
            builder.setNegativeButton("取消", null); // 取消操作不执行任何动作
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        });

        return view;

    }

}